<!DOCTYPE html>


<html lang="en">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    vue3.0 |  
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
      <canvas width="1777" height="841"
        style="position: fixed; left: 0px; top: 0px; z-index: 99999; pointer-events: none;"></canvas>
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-vue3"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  vue3.0
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/10/09/vue3/" class="article-date">
  <time datetime="2020-10-09T09:30:00.000Z" itemprop="datePublished">2020-10-09</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/vue/">vue</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">4.9k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">23 min</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <blockquote>
<p>结合<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV14k4y117LL">李江南老师的视频</a>和<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1L5411j7vj">技术胖的视频</a>和文档进行二次编辑的</p>
</blockquote>
<h2 id="Vue3-0亮点"><a href="#Vue3-0亮点" class="headerlink" title="Vue3.0亮点"></a>Vue3.0亮点</h2><ul>
<li>Performance：性能比Vue 2.x快1.2~2倍</li>
<li>Tree shaking support：按需编译,体积比Vue2.x更小</li>
<li>Composition API: 组合API(类似React Hooks)</li>
<li>Better TypeScript support：更好的 Ts 支持</li>
<li>Custom Renderer API：暴露了自定义渲染API</li>
<li>Fragment, Teleport(Protal), Suspense：更先进的组件</li>
</ul>
<h2 id="Vue3-0是如何变快的"><a href="#Vue3-0是如何变快的" class="headerlink" title="Vue3.0是如何变快的"></a>Vue3.0是如何变快的</h2><h3 id="diff算法优化-https-vue-next-template-explorer-netlify-app"><a href="#diff算法优化-https-vue-next-template-explorer-netlify-app" class="headerlink" title="diff算法优化: https://vue-next-template-explorer.netlify.app/"></a>diff算法优化: <a target="_blank" rel="noopener" href="https://vue-next-template-explorer.netlify.app/">https://vue-next-template-explorer.netlify.app/</a></h3><blockquote>
<ul>
<li>Vue2中的虚拟dom是进行全量的对比  </li>
<li>Vue3新增了<code>非静态标记</code>(PatchFlag)  <ol>
<li>在与上次虚拟节点进行对比时候，只对比带有patch flag的节点</li>
<li>并且可以通过flag的信息得知当前节点要对比的具体内容</li>
</ol>
</li>
</ul>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">// template内的内容</span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>Hello World!<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h1</span> <span class="attr">:title</span>=<span class="string">&#x27;haha&#x27;</span> <span class="attr">:value</span>=<span class="string">&#x27;hh&#x27;</span>&gt;</span>&#123;&#123;msg&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 源码</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">_ctx, _cache, $props, $setup, $data, $options</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (_openBlock(), _createBlock(_Fragment, <span class="literal">null</span>, [</span><br><span class="line">    _createVNode(<span class="string">&quot;div&quot;</span>, <span class="literal">null</span>, <span class="string">&quot;Hello World!&quot;</span>),</span><br><span class="line">    _createVNode(<span class="string">&quot;h1&quot;</span>, &#123;</span><br><span class="line">      title: _ctx.haha,</span><br><span class="line">      value: _ctx.hh</span><br><span class="line">    &#125;, _toDisplayString(_ctx.msg), <span class="number">9</span> <span class="comment">/* TEXT, PROPS */</span>, [<span class="string">&quot;title&quot;</span>, <span class="string">&quot;value&quot;</span>])</span><br><span class="line">  ], <span class="number">64</span> <span class="comment">/* STABLE_FRAGMENT */</span>))</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">不需要遍历节点一一对比</span></span><br><span class="line"><span class="comment">对动态的组件进行标记（只比较标记的内容）</span></span><br><span class="line"><span class="comment">数字的不同表示了 比较的内容不同</span></span><br><span class="line"><span class="comment">不需要针对整个节点去改变，只需要针对特定的内容去重新渲染</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">9 ---&gt; 代表他动态的内容为 text 和 props</span></span><br><span class="line"><span class="comment">改变的时候只需要改变文本内容和属性</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h3 id="hoistStatic-静态提升"><a href="#hoistStatic-静态提升" class="headerlink" title="hoistStatic 静态提升"></a>hoistStatic 静态提升</h3><blockquote>
<ul>
<li>Vue2中无论元素是否参与更新, 每次都会重新创建, 然后再渲染</li>
<li>Vue3中对于不参与更新的元素, 会做静态提升, 只会被创建一次, 在渲染时直接复用即可</li>
</ul>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">// template内的内容</span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>Hello World!<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h1</span> <span class="attr">:title</span>=<span class="string">&#x27;haha&#x27;</span> <span class="attr">:value</span>=<span class="string">&#x27;hh&#x27;</span>&gt;</span>&#123;&#123;msg&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 源码</span></span><br><span class="line"><span class="comment">// 静态提升之前</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">_ctx, _cache, $props, $setup, $data, $options</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (_openBlock(), _createBlock(_Fragment, <span class="literal">null</span>, [</span><br><span class="line">    _createVNode(<span class="string">&quot;div&quot;</span>, <span class="literal">null</span>, <span class="string">&quot;Hello World!&quot;</span>),</span><br><span class="line">    _createVNode(<span class="string">&quot;h1&quot;</span>, &#123;</span><br><span class="line">      title: _ctx.haha,</span><br><span class="line">      value: _ctx.hh</span><br><span class="line">    &#125;, _toDisplayString(_ctx.msg), <span class="number">9</span> <span class="comment">/* TEXT, PROPS */</span>, [<span class="string">&quot;title&quot;</span>, <span class="string">&quot;value&quot;</span>])</span><br><span class="line">  ], <span class="number">64</span> <span class="comment">/* STABLE_FRAGMENT */</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 静态提升之后</span></span><br><span class="line"><span class="keyword">const</span> _hoisted_1 = <span class="comment">/*#__PURE__*/</span>_createVNode(<span class="string">&quot;div&quot;</span>, <span class="literal">null</span>, <span class="string">&quot;Hello World!&quot;</span>, <span class="number">-1</span> <span class="comment">/* HOISTED */</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">_ctx, _cache, $props, $setup, $data, $options</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (_openBlock(), _createBlock(_Fragment, <span class="literal">null</span>, [</span><br><span class="line">    _hoisted_1,</span><br><span class="line">    _createVNode(<span class="string">&quot;h1&quot;</span>, &#123;</span><br><span class="line">      title: _ctx.haha,</span><br><span class="line">      value: _ctx.hh</span><br><span class="line">    &#125;, _toDisplayString(_ctx.msg), <span class="number">9</span> <span class="comment">/* TEXT, PROPS */</span>, [<span class="string">&quot;title&quot;</span>, <span class="string">&quot;value&quot;</span>])</span><br><span class="line">  ], <span class="number">64</span> <span class="comment">/* STABLE_FRAGMENT */</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">每次render的时候全部内容会重新创建一次，会造成额外的内存消耗</span></span><br><span class="line"><span class="comment">通过把静态的节点提取为全局变量</span></span><br><span class="line"><span class="comment">所以就只需要创建一次</span></span><br><span class="line"><span class="comment">组件渲染的时候只需要调用就行了</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h3 id="cacheHandlers-事件侦听器缓存"><a href="#cacheHandlers-事件侦听器缓存" class="headerlink" title="cacheHandlers 事件侦听器缓存"></a>cacheHandlers 事件侦听器缓存</h3><ul>
<li>默认情况下onClick会被视为动态绑定, 所以每次都会去追踪它的变化<pre><code>但是因为是同一个函数，所以没有追踪变化, 直接缓存起来复用即可</code></pre>
</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&#x27;handleClick&#x27;</span>&gt;</span>按钮<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 开启事件侦听之前</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">_ctx, _cache, $props, $setup, $data, $options</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (_openBlock(), _createBlock(<span class="string">&quot;button&quot;</span>, &#123; <span class="attr">onClick</span>: _ctx.handleClick &#125;, <span class="string">&quot;按钮&quot;</span>, <span class="number">8</span> <span class="comment">/* PROPS */</span>, [<span class="string">&quot;onClick&quot;</span>]))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 开启事件侦听之后</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">_ctx, _cache, $props, $setup, $data, $options</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (_openBlock(), _createBlock(<span class="string">&quot;button&quot;</span>, &#123;</span><br><span class="line">    onClick: _cache[<span class="number">1</span>] || (_cache[<span class="number">1</span>] = <span class="function">(<span class="params">...args</span>) =&gt;</span> (_ctx.handleClick(...args)))</span><br><span class="line">  &#125;, <span class="string">&quot;按钮&quot;</span>))</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">开启之前，通过静态标记的形式来实现 ==&gt; 比较属性（但是方法是不变的）</span></span><br><span class="line"><span class="comment">开启之后，不使用静态标记，所以不会进行比较。通过一个变量对方法进行缓存，下次在调用的时候直接从缓存中获取，不去再次创建整个函数</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h3 id="ssr渲染"><a href="#ssr渲染" class="headerlink" title="ssr渲染"></a>ssr渲染</h3><ul>
<li><p>当有大量静态的内容时候，这些内容会被当做纯字符串推进一个buffer里面，即使存在动态的绑定，会通过模板插值嵌入进去。这样会比通过虚拟dmo来渲染的快上很多很多。</p>
</li>
<li><p>当静态内容大到一定量级时候，会用_createStaticVNode方法在客户端去生成一个static node，这些静态node，会被直接innerHtml，就不需要创建对象，然后根据对象渲染。</p>
</li>
</ul>
<h2 id="组合API（composition-api）"><a href="#组合API（composition-api）" class="headerlink" title="组合API（composition api）"></a>组合API（composition api）</h2><blockquote>
<p>vue2.x实现列表的增删功能</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">实现一个功能就需要</span></span><br><span class="line"><span class="comment">在data内定义数据，</span></span><br><span class="line"><span class="comment">在methods / computed / watch 中实现业务逻辑来操作数据</span></span><br><span class="line"><span class="comment">造成 数据和业务逻辑 分散</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;form&gt;</span><br><span class="line">      &lt;input placeholder=<span class="string">&quot;id&quot;</span> type=<span class="string">&quot;text&quot;</span>  v-model=<span class="string">&quot;info.id&quot;</span> /&gt;</span><br><span class="line">      &lt;br&gt;</span><br><span class="line">      &lt;input placeholder=<span class="string">&quot;name&quot;</span> type=<span class="string">&quot;text&quot;</span> v-model=<span class="string">&quot;info.name&quot;</span> /&gt;</span><br><span class="line">      &lt;br&gt;</span><br><span class="line">      &lt;input type=<span class="string">&quot;submit&quot;</span> @click=<span class="string">&quot;handleAdd&quot;</span> /&gt;</span><br><span class="line">    &lt;/form&gt;</span><br><span class="line">    &lt;ul&gt;</span><br><span class="line">      &lt;li</span><br><span class="line">        v-<span class="keyword">for</span>=<span class="string">&quot;(item,index) in list&quot;</span></span><br><span class="line">        :key=<span class="string">&quot;item.id&quot;</span></span><br><span class="line">        @click=<span class="string">&quot;handleRemove(index)&quot;</span></span><br><span class="line">      &gt;&#123;&#123;item.name&#125;&#125; ----- &#123;&#123; index &#125;&#125;&lt;/li&gt;</span><br><span class="line">    &lt;/ul&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">&quot;App&quot;</span>,</span><br><span class="line">  data() &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      info: &#123;</span><br><span class="line">        id:<span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        name:<span class="string">&#x27;&#x27;</span></span><br><span class="line">      &#125;,</span><br><span class="line">      list: [</span><br><span class="line">        &#123; <span class="attr">id</span>: <span class="number">0</span>, <span class="attr">name</span>: <span class="string">&quot;jjj&quot;</span> &#125;,</span><br><span class="line">        &#123; <span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">&quot;hhh&quot;</span> &#125;,</span><br><span class="line">        &#123; <span class="attr">id</span>: <span class="number">2</span>, <span class="attr">name</span>: <span class="string">&quot;0hh&quot;</span> &#125;,</span><br><span class="line">      ],</span><br><span class="line">      itemRefs: [],</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    handleRemove(index) &#123;</span><br><span class="line">      <span class="built_in">this</span>.list.splice(index, <span class="number">1</span>);</span><br><span class="line">    &#125;,</span><br><span class="line">    handleAdd(e)&#123;</span><br><span class="line">      e.preventDefault();</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="built_in">this</span>.info)</span><br><span class="line">      <span class="built_in">this</span>.list.push(<span class="built_in">this</span>.info)</span><br><span class="line">      <span class="built_in">this</span>.info = &#123;</span><br><span class="line">        id:<span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        name:<span class="string">&#x27;&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p>vue3组合API的使用</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;h1&gt;&#123;&#123;count&#125;&#125;&lt;/h1&gt;</span><br><span class="line">    &lt;button @click=<span class="string">&quot;handleAdd&quot;</span>&gt;添加&lt;/button&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="comment">// 只能监听简单类型的变化，不能监听复杂类型的变化</span></span><br><span class="line"><span class="keyword">import</span> &#123; ref &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 监听复杂类型的变化</span></span><br><span class="line"><span class="keyword">import</span> &#123; reactive &#125; form <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">&quot;App&quot;</span>,</span><br><span class="line"></span><br><span class="line">  <span class="comment">// setup为组合函数的入口函数</span></span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="comment">// 定义变量和初始化变量 count是一个对象，value是他的值</span></span><br><span class="line">    <span class="keyword">let</span> count = ref(<span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 定义方法</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleAdd</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">      count.value ++</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 暴露变量和方法以供使用</span></span><br><span class="line">    <span class="keyword">return</span> &#123; count,handleAdd &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 可以将setup里面的内容单独抽离出来</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">&quot;App&quot;</span>,</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> &#123; obj, handleRemove &#125; = useRemove();</span><br><span class="line">    <span class="keyword">return</span> &#123; obj, handleRemove &#125;;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">useRemove</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> obj = reactive(&#123;</span><br><span class="line">    arr: [</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="string">&quot;1&quot;</span>, <span class="attr">name</span>: <span class="string">&quot;dorren&quot;</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="string">&quot;2&quot;</span>, <span class="attr">name</span>: <span class="string">&quot;jerry&quot;</span> &#125;,</span><br><span class="line">    ],</span><br><span class="line">  &#125;);</span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">handleRemove</span>(<span class="params">index</span>) </span>&#123;</span><br><span class="line">    obj.arr.splice(index, <span class="number">1</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> &#123; obj, handleRemove &#125;;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 使用的时候通过obj.arr使用</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 抽离方式2</span></span><br><span class="line"><span class="keyword">import</span> &#123; time, handleChange &#125; <span class="keyword">from</span> <span class="string">&quot;./hooks/useGetTime.ts&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123; time, handleChange &#125;;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// ./hooks/useGetTime.ts</span></span><br><span class="line"><span class="keyword">import</span> &#123; ref &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">let</span> time = ref(<span class="string">&quot;00:00:00&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> handleChange = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> date = <span class="keyword">new</span> <span class="built_in">Date</span>();</span><br><span class="line">    <span class="keyword">const</span> hour =</span><br><span class="line">        date.getHours() &lt; <span class="number">10</span> ? <span class="string">&quot;0&quot;</span> + date.getHours() : date.getHours();</span><br><span class="line">    <span class="keyword">const</span> min =</span><br><span class="line">        date.getMinutes() &lt; <span class="number">10</span> ? <span class="string">&quot;0&quot;</span> + date.getMinutes() : date.getMinutes();</span><br><span class="line">    <span class="keyword">const</span> sec =</span><br><span class="line">        date.getSeconds() &lt; <span class="number">10</span> ? <span class="string">&quot;0&quot;</span> + date.getSeconds() : date.getSeconds();</span><br><span class="line">    time.value = hour + <span class="string">&quot;:&quot;</span> + min + <span class="string">&quot;:&quot;</span> + sec;</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">        handleChange()</span><br><span class="line">    &#125;, <span class="number">1000</span>);</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">export</span> &#123; time, handleChange &#125;</span><br></pre></td></tr></table></figure>

<p>组合API完整示例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;form&gt;</span><br><span class="line">      &lt;input placeholder=<span class="string">&quot;id&quot;</span> type=<span class="string">&quot;text&quot;</span> v-model=<span class="string">&quot;info.info.id&quot;</span> /&gt;</span><br><span class="line">      &lt;br /&gt;</span><br><span class="line">      &lt;input placeholder=<span class="string">&quot;name&quot;</span> type=<span class="string">&quot;text&quot;</span> v-model=<span class="string">&quot;info.info.name&quot;</span> /&gt;</span><br><span class="line">      &lt;br /&gt;</span><br><span class="line">      &lt;button @click=<span class="string">&quot;handleAdd&quot;</span>&gt;添加&lt;/button&gt;</span><br><span class="line">    &lt;/form&gt;</span><br><span class="line">    &lt;h1</span><br><span class="line">      v-<span class="keyword">for</span>=<span class="string">&quot;(item,index) in obj.arr&quot;</span></span><br><span class="line">      :key=<span class="string">&quot;item.id&quot;</span></span><br><span class="line">      @click=<span class="string">&quot;handleRemove(index)&quot;</span></span><br><span class="line">    &gt;&#123;&#123;item.name&#125;&#125;---&#123;&#123;item.id&#125;&#125;&lt;/h1&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">import</span> &#123; reactive &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">&quot;App&quot;</span>,</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> &#123; obj, handleRemove &#125; = useRemove();</span><br><span class="line">    <span class="comment">// 参数传递</span></span><br><span class="line">    <span class="keyword">let</span> &#123; info, handleAdd &#125; = useAdd(obj);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123; obj, handleRemove, info, handleAdd &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">useRemove</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> obj = reactive(&#123;</span><br><span class="line">    arr: [</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="string">&quot;1&quot;</span>, <span class="attr">name</span>: <span class="string">&quot;dorren&quot;</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="string">&quot;2&quot;</span>, <span class="attr">name</span>: <span class="string">&quot;jerry&quot;</span> &#125;,</span><br><span class="line">    ],</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">handleRemove</span>(<span class="params">index</span>) </span>&#123;</span><br><span class="line">    obj.arr.splice(index, <span class="number">1</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> &#123; obj, handleRemove &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">useAdd</span>(<span class="params">obj</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> info = reactive(&#123;</span><br><span class="line">    info: &#123;</span><br><span class="line">      id: <span class="string">&quot;&quot;</span>,</span><br><span class="line">      name: <span class="string">&quot;&quot;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;);</span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">handleAdd</span>(<span class="params">e</span>) </span>&#123;</span><br><span class="line">    e.preventDefault();</span><br><span class="line">    <span class="keyword">const</span> item = <span class="built_in">Object</span>.assign(&#123;&#125;, info.info);</span><br><span class="line">    obj.arr.push(item);</span><br><span class="line">    info.info.id = <span class="string">&quot;&quot;</span>;</span><br><span class="line">    info.info.name = <span class="string">&quot;&quot;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> &#123; info, handleAdd &#125;;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="组合API内使用生命周期钩子"><a href="#组合API内使用生命周期钩子" class="headerlink" title="组合API内使用生命周期钩子"></a>组合API内使用生命周期钩子</h3><table>
<thead>
<tr>
<th>选项API</th>
<th>Hook inside <code>setup</code></th>
</tr>
</thead>
<tbody><tr>
<td><code>beforeMount</code></td>
<td><code>onBeforeMount</code></td>
</tr>
<tr>
<td><code>mounted</code></td>
<td><code>onMounted</code></td>
</tr>
<tr>
<td><code>beforeUpdate</code></td>
<td><code>onBeforeUpdate</code></td>
</tr>
<tr>
<td><code>updated</code></td>
<td><code>onUpdated</code></td>
</tr>
<tr>
<td><code>beforeUnmount</code></td>
<td><code>onBeforeUnmount</code>vue2的beforeDestroy</td>
</tr>
<tr>
<td><code>unmounted</code></td>
<td><code>onUnmounted</code></td>
</tr>
<tr>
<td><code>errorCaptured</code></td>
<td><code>onErrorCaptured</code>捕获子组件异常</td>
</tr>
<tr>
<td><code>renderTracked</code></td>
<td><code>onRenderTracked</code>对变化数据进行跟踪</td>
</tr>
<tr>
<td><code>renderTriggered</code></td>
<td><code>onRenderTriggered</code>精确跟踪触发(有新旧值))</td>
</tr>
</tbody></table>
<h3 id="reactive"><a href="#reactive" class="headerlink" title="reactive"></a>reactive</h3><ul>
<li>vue3提供的实现响应式数据的方法  </li>
<li>用es6的proxy取代了defineProperty来实现响应式</li>
</ul>
<p>tips：</p>
<ul>
<li>reactive参数必须是对象（json/arr）</li>
<li>如果给reactive传递了其他类型<ul>
<li>默认情况下修改值，界面不会自动更新</li>
<li>如果想更新，可以通过重新赋值的方式</li>
</ul>
</li>
</ul>
<h3 id="ref"><a href="#ref" class="headerlink" title="ref"></a>ref</h3><ul>
<li>vue3提供的对简单值的监听</li>
<li>本质是reactive ref(xx) —&gt; reactive({ value: xx })</li>
</ul>
<p>tips:</p>
<ul>
<li>在vue中使用ref的值不用通过value获取 </li>
<li>在js中使用ref的值必须通过value获取  msg.value = 111</li>
</ul>
<h3 id="isRef和isReactive"><a href="#isRef和isReactive" class="headerlink" title="isRef和isReactive"></a>isRef和isReactive</h3><blockquote>
<p>用于判断变量类型</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; isRef,isReactive &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"></span><br><span class="line">setup() &#123;</span><br><span class="line">  <span class="keyword">let</span> num = ref(<span class="number">99</span>)</span><br><span class="line">  <span class="built_in">console</span>.log( isRef(num) )  <span class="comment">// true</span></span><br><span class="line">  <span class="built_in">console</span>.log( isReactive(num) )  <span class="comment">// false</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="组件通信"><a href="#组件通信" class="headerlink" title="组件通信"></a>组件通信</h3><h4 id="父传子-props"><a href="#父传子-props" class="headerlink" title="父传子( props )"></a>父传子( props )</h4><blockquote>
<p>基本和vue2.x一样，父组件绑定属性，子组件通过props获取</p>
</blockquote>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 父组件</span></span><br><span class="line">&lt;child-component :name=<span class="string">&quot;name&quot;</span> :age=<span class="string">&quot;count&quot;</span>  /&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 子组件</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span>&#123;</span><br><span class="line">  props:&#123;</span><br><span class="line">    name:&#123;</span><br><span class="line">      <span class="keyword">type</span>:<span class="built_in">String</span>,  <span class="comment">// 类型 String、Number、Boolean、Array、Object、Date、Function、Symbol</span></span><br><span class="line">      required:<span class="literal">true</span>,    <span class="comment">// 是否必传 true、false</span></span><br><span class="line">      <span class="keyword">default</span>:<span class="string">&#x27;jerry&#x27;</span>,  <span class="comment">// 默认值 对象或数组默认值必须从一个工厂函数获取 ()=&gt;...</span></span><br><span class="line"></span><br><span class="line">      <span class="comment">//  自定义验证函数 </span></span><br><span class="line">      validator:<span class="function">(<span class="params">value:<span class="built_in">string</span></span>)=&gt;</span> <span class="keyword">return</span> [<span class="string">&#x27;sherry&#x27;</span>,<span class="string">&#x27;jerry&#x27;</span>,<span class="string">&#x27;doreen&#x27;</span>].indexOf(value)!==<span class="number">-1</span></span><br><span class="line">    &#125;,</span><br><span class="line">    age:&#123;</span><br><span class="line">      <span class="comment">// ...</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 通过参数、在setup中使用</span></span><br><span class="line">  setup:<span class="function">(<span class="params">props</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="comment">// props是通过ref包裹的 响应式引用</span></span><br><span class="line">    <span class="built_in">console</span>.log(props.name)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="子传父-emit"><a href="#子传父-emit" class="headerlink" title="子传父( emit )"></a>子传父( emit )</h4><blockquote>
<p>有细微的差别</p>
</blockquote>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 子组件</span></span><br><span class="line">&lt;button <span class="meta">@click</span>=<span class="string">&#x27;handleEmit&#x27;</span>&gt; 按钮 &lt;/button&gt;</span><br><span class="line"></span><br><span class="line">setup:<span class="function">(<span class="params">props,context</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="comment">// context 不是响应式的 可写为 &#123; attrs,slots,emit &#125; Attribute,插槽,广播</span></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">handleEmit</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    context.emit(<span class="string">&#x27;toParentComponent&#x27;</span>,<span class="string">&#x27;some info&#x27;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> &#123; handleEmit &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 父组件</span></span><br><span class="line">&lt;child-component <span class="meta">@toParentComponent</span>=<span class="string">&#x27;hanldeFun&#x27;</span> /&gt;</span><br><span class="line"></span><br><span class="line">setup:<span class="function">()=&gt;</span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">handleFun</span>(<span class="params">value:<span class="built_in">string</span></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(value)  <span class="comment">// some info</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> &#123; handleFun &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="父传孙子-provide-inject"><a href="#父传孙子-provide-inject" class="headerlink" title="父传孙子( provide inject )"></a>父传孙子( provide inject )</h4><blockquote>
<p>父组件通过<code>provide</code>传递，孙子组件通过<code>inject</code>接收</p>
</blockquote>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">// 父组件 传递</span></span><br><span class="line">setup: <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  provide(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;value&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 子组件 接收</span></span><br><span class="line">setup: <span class="function">(<span class="params">props, context</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> result = inject(<span class="string">&quot;name&quot;</span>);</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<h2 id="递归监听"><a href="#递归监听" class="headerlink" title="递归监听"></a>递归监听</h2><blockquote>
<p>无论是通过ref还是reactive创建的数据都是递归监听<br>能够监听每一层数据 并且把每一层数据都包装成proxy对象</p>
</blockquote>
<h2 id="非递归监听"><a href="#非递归监听" class="headerlink" title="非递归监听"></a>非递归监听</h2><blockquote>
<p><code>shallowReactive</code> 和 <code>shallowRef</code></p>
<ul>
<li>只为某个对象的私有（第一层）属性创建浅层的响应式代理，不会对“属性的属性”做深层次、递归地响应式代理，而只是保留原样。</li>
<li>只有第一层被封装成proxy对象，其余内容都是对象的形式存储，如果第一层内容没有被改变，就不会触发页面的更新</li>
</ul>
</blockquote>
<blockquote>
<p>Tips:<br>如果是通过<code>shallowRef</code>创建的数据，vue监听的是<code>.value</code>的变化，并不是第一层的变化。</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>&#123;&#123;date.data&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>&#123;&#123;date.obj.name&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>&#123;&#123;date.obj.son.name&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;handleAdd&quot;</span>&gt;</span>添加<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; shallowReactive &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">&quot;App&quot;</span>,</span><br><span class="line">  data() &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;&#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> date = shallowReactive(&#123;</span><br><span class="line">      date: <span class="string">&#x27;hhh&#x27;</span>,</span><br><span class="line">      obj: &#123;</span><br><span class="line">        name:<span class="string">&#x27;doreen&#x27;</span>,</span><br><span class="line">        son: &#123;</span><br><span class="line">          name: <span class="string">&#x27;jerry&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleAdd</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="comment">// 第一层数据改变，点击事件后，页面数据更新</span></span><br><span class="line">      date.data = <span class="string">&#x27;eee&#x27;</span></span><br><span class="line">      date.obj.name = <span class="string">&#x27;sherry&#x27;</span></span><br><span class="line">      date.obj.son.name = <span class="string">&#x27;kris&#x27;</span></span><br><span class="line">      </span><br><span class="line">      <span class="comment">// 第一层数据没有改变，点击事件后，数据改变页面没有更新</span></span><br><span class="line">      date.obj.name = <span class="string">&#x27;sherry&#x27;</span></span><br><span class="line">      date.obj.son.name = <span class="string">&#x27;kris&#x27;</span></span><br><span class="line">      </span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123; date, handleAdd &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="triggerRef"><a href="#triggerRef" class="headerlink" title="triggerRef"></a>triggerRef</h2><blockquote>
<p>针对<code>ref</code>类型的数据，vue3提供了<code>triggerRef</code>的方法进行触发更新  </p>
</blockquote>
<p>（对于<code>reactive</code>类型的数据没有提供相应方法）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; triggerRef &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line">...</span><br><span class="line"></span><br><span class="line">date.value.son.name = <span class="string">&#x27;kris&#x27;</span></span><br><span class="line">triggerRef(date);</span><br><span class="line"></span><br><span class="line">...</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="toRow"><a href="#toRow" class="headerlink" title="toRow"></a>toRow</h2><blockquote>
<p>获取ref / reactive 的原始数据  </p>
</blockquote>
<p><code>ref</code> / <code>reactive</code> 数据类型的特点:<br>每次修改都会被追踪，触发界面更新，<code>但是会非常消耗性能</code><br>对于一些不需要追踪的操作，不需要更新界面，这个时候<br>就可以用<code>toRow</code>方法拿到他的原始数据，对原始数据进行修改<br>这样就不会被追踪并且不会更新界面，提高了性能。 </p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; reactive,toRow &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> obj = &#123; <span class="attr">name</span>: <span class="string">&quot;doreen&quot;</span>,<span class="attr">sex</span>: <span class="string">&quot;女&quot;</span> &#125;;</span><br><span class="line">    <span class="keyword">let</span> state = reactive(obj);</span><br><span class="line">    <span class="keyword">let</span> obj2 = toRaw(state); <span class="comment">// 获取ref / reactive 的原始数据</span></span><br><span class="line">    <span class="comment">// let state = ref(obj);</span></span><br><span class="line">    <span class="comment">// let obj2 = toRow(state.value)  // ref需要通过.value获取原始数据</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">// console.log(obj === obj2); // true</span></span><br><span class="line">    <span class="comment">// console.log( obj === state )    false</span></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    state 和 obj ：</span></span><br><span class="line"><span class="comment">    引用关系，state本质是一个proxy对象，在这个proxy对象中引用了obj</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleChange</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="comment">/*</span></span><br><span class="line"><span class="comment">      直接修改 obj 无法触发更新</span></span><br><span class="line"><span class="comment">      通过proxy对象包装后，才能触发页面更新</span></span><br><span class="line"><span class="comment">      */</span></span><br><span class="line">      obj.name = <span class="string">&quot;sherry&quot;</span>;</span><br><span class="line">      <span class="built_in">console</span>.log(obj);    <span class="comment">// &#123; name:&#x27;sherry&#x27;,sex:&#x27;女&#x27; &#125;</span></span><br><span class="line">      <span class="built_in">console</span>.log(state);  <span class="comment">// &#123; name:&#x27;sherry&#x27;,sex:&#x27;女&#x27; &#125;</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123; obj, state, handleChange &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="markRow"><a href="#markRow" class="headerlink" title="markRow"></a>markRow</h2><blockquote>
<p>让数据永远不会被追踪</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> obj = markRaw(&#123;</span><br><span class="line">      name: <span class="string">&quot;doreen&quot;</span>,</span><br><span class="line">      sex: <span class="string">&quot;女&quot;</span>,</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">let</span> state = ref(obj);</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleChange</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="comment">// 不触发页面更新</span></span><br><span class="line">      obj.name = <span class="string">&quot;sherry&quot;</span>;</span><br><span class="line">      <span class="built_in">console</span>.log(obj); <span class="comment">// &#123; name:&#x27;sherry&#x27;,sex:&#x27;女&#x27; &#125;</span></span><br><span class="line">      <span class="built_in">console</span>.log(state); <span class="comment">// &#123; name:&#x27;sherry&#x27;,sex:&#x27;女&#x27; &#125;</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123; state, handleChange &#125;;</span><br><span class="line">  &#125;,</span><br></pre></td></tr></table></figure>

<h2 id="toRef"><a href="#toRef" class="headerlink" title="toRef"></a>toRef</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> obj = &#123;</span><br><span class="line">      name: <span class="string">&quot;doreen&quot;</span>,</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * let state = ref(obj.name)</span></span><br><span class="line"><span class="comment">     * =&gt; let state = ref(&#x27;doreen&#x27;)</span></span><br><span class="line"><span class="comment">     * =&gt; let state = reactive(&#123; value: &#x27;doreen&#x27; &#125;)</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">let</span> state = ref(obj.name);</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleChange</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      state.value = <span class="string">&quot;sherry&quot;</span>;</span><br><span class="line">      <span class="comment">/**</span></span><br><span class="line"><span class="comment">       * 如果利用ref将某一个对象中的属性变成响应式的数据</span></span><br><span class="line"><span class="comment">       * 我们修改的响应式数据不会影响到原始数据</span></span><br><span class="line"><span class="comment">       */</span></span><br><span class="line">      <span class="built_in">console</span>.log(obj); <span class="comment">// &#123; name:&#x27;doreen&#x27; &#125;</span></span><br><span class="line">      <span class="built_in">console</span>.log(state); <span class="comment">// &#123; value: &#x27;sherry&#x27; &#125;</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123; state, handleChange &#125;;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> obj = &#123;</span><br><span class="line">      name: <span class="string">&quot;doreen&quot;</span>,</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    ref和toRef的区别：</span></span><br><span class="line"><span class="comment">    ref -&gt; 复制，修改响应式数据不会影响以前的数据</span></span><br><span class="line"><span class="comment">           数据发生改变，界面就会自动更新</span></span><br><span class="line"><span class="comment">    </span></span><br><span class="line"><span class="comment">    toRef -&gt; 引用，修改响应式数据会影响以前的数据</span></span><br><span class="line"><span class="comment">             数据发生改变，界面也不会自动更新</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="keyword">let</span> state = toRef(obj,<span class="string">&#x27;name&#x27;</span>);</span><br><span class="line">    <span class="built_in">console</span>.log(state)</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleChange</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      state.value = <span class="string">&quot;sherry&quot;</span>;</span><br><span class="line">      <span class="comment">/**</span></span><br><span class="line"><span class="comment">       * 如果利用toRef将某一个对象中的属性变成响应式的数据</span></span><br><span class="line"><span class="comment">       * 我们修改的响应式数据会影响到原始数据</span></span><br><span class="line"><span class="comment">       * 如果响应式的数据通过toRef创建的，修改数据并不会触发界面更新</span></span><br><span class="line"><span class="comment">       */</span></span><br><span class="line">      <span class="built_in">console</span>.log(obj); <span class="comment">// &#123; name:&#x27;sherry&#x27; &#125;</span></span><br><span class="line">      <span class="built_in">console</span>.log(state); <span class="comment">// &#123; value: &#x27;sherry&#x27; &#125;</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123; state, handleChange &#125;;</span><br><span class="line">  &#125;,</span><br></pre></td></tr></table></figure>

<h2 id="customRef"><a href="#customRef" class="headerlink" title="customRef"></a>customRef</h2><blockquote>
<p>返回一个ref对象，可以显式的控制依赖追踪和触发响应</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; customRef &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 自定义 ref</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myRef</span>(<span class="params">value</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// track -&gt; 追踪  trigger -&gt; 触发</span></span><br><span class="line">  <span class="comment">// 没有这两个参数，ui界面就不会被触发更新</span></span><br><span class="line">  <span class="keyword">return</span> customRef(<span class="function">(<span class="params">track,trigger</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      get() &#123;</span><br><span class="line">        track() <span class="comment">// 告诉vue 数据需要被追踪变化的 </span></span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;get:&quot;</span>, value);</span><br><span class="line">        <span class="keyword">return</span> value;</span><br><span class="line">      &#125;,</span><br><span class="line">      set(newV) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;set:&quot;</span>, newV);</span><br><span class="line">        value = newV</span><br><span class="line">        trigger() <span class="comment">// 触发更新</span></span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> state = myRef(<span class="number">15</span>);</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleChange</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      state.value++;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> &#123; state, handleChange &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p>应用场景：<br>setup中不能进行异步操作<br>如果需要通过网络请求对数据进行赋值<br>就可以使用<code>customRef</code>来获取请求数据</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">import</span> &#123; customRef &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"> <span class="function"><span class="keyword">function</span> <span class="title">myRef</span>(<span class="params">value</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// track -&gt; 追踪  trigger -&gt; 触发</span></span><br><span class="line">  <span class="keyword">return</span> customRef(<span class="function">(<span class="params">track, trigger</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 在get之前对数据进行赋值 不会造成重复刷新</span></span><br><span class="line">    fetch(value)</span><br><span class="line">      .then(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">JSON</span>.json();</span><br><span class="line">      &#125;)</span><br><span class="line">      .then(<span class="function">(<span class="params">data</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(data);</span><br><span class="line">        value = data;</span><br><span class="line">        trigger(); <span class="comment">// 获取数据、刷新页面</span></span><br><span class="line">      &#125;)</span><br><span class="line">      .catch(<span class="function">(<span class="params">err</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(err);</span><br><span class="line">      &#125;);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      get() &#123;</span><br><span class="line">        track(); <span class="comment">// 数据需要被追踪变化的</span></span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;get:&quot;</span>, value);</span><br><span class="line">        <span class="keyword">return</span> value;</span><br><span class="line">      &#125;,</span><br><span class="line">      set(newV) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;set:&quot;</span>, newV);</span><br><span class="line">        value = newV;</span><br><span class="line">        trigger(); <span class="comment">// 触发更新</span></span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">&#125; </span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> state = myRef(<span class="string">&#x27;../public/data.json&#x27;</span>);</span><br><span class="line">    <span class="keyword">return</span> &#123; state &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;; </span><br></pre></td></tr></table></figure>

<h2 id="获取元素"><a href="#获取元素" class="headerlink" title="获取元素"></a>获取元素</h2><blockquote>
<p>vue2x中通过绑定ref属性，然后通过$refs.xxx来获取元素<br>vue3取消了$refs,但是也可以通过ref来获取元素</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&lt;h1 ref=<span class="string">&quot;box&quot;</span>&gt;&lt;/h1&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; ref, onMounted &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> box = ref(<span class="literal">null</span>);</span><br><span class="line">    </span><br><span class="line">    onMounted(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      <span class="built_in">console</span>.log(box.value); <span class="comment">// &lt;h1&gt;&lt;/h1&gt;</span></span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="comment">// setup的生命周期在beforeCreated之前 </span></span><br><span class="line">    <span class="built_in">console</span>.log(box.value) <span class="comment">// null</span></span><br><span class="line">    <span class="keyword">return</span> &#123; box &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>


<h2 id="readonly"><a href="#readonly" class="headerlink" title="readonly"></a>readonly</h2><blockquote>
<p>用于创建一个只读的数据，并且递归只读</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; readonly &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> box = readonly(&#123;</span><br><span class="line">      name: <span class="string">&quot;doreen&quot;</span>,</span><br><span class="line">      attr: &#123;</span><br><span class="line">        age: <span class="number">12</span>,</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleChange</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      box.name = <span class="string">&quot;sherry&quot;</span>;</span><br><span class="line">      box.attr.age = <span class="number">22</span>;</span><br><span class="line"></span><br><span class="line">      <span class="built_in">console</span>.log(box);</span><br><span class="line">      <span class="comment">/**</span></span><br><span class="line"><span class="comment">       * &#123;</span></span><br><span class="line"><span class="comment">          name: &quot;doreen&quot;,</span></span><br><span class="line"><span class="comment">          attr: &#123;</span></span><br><span class="line"><span class="comment">              age: 12,</span></span><br><span class="line"><span class="comment">          &#125;,</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">       */</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> &#123; box, handleChange &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="shallowReadonly"><a href="#shallowReadonly" class="headerlink" title="shallowReadonly"></a>shallowReadonly</h2><blockquote>
<p>创建一个<code>第一层</code>只读的数据 (非递归)</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; shallowReadonly &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> box = shallowReadonly(&#123;</span><br><span class="line">      name: <span class="string">&quot;doreen&quot;</span>,</span><br><span class="line">      attr: &#123;</span><br><span class="line">        age: <span class="number">12</span>,</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleChange</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      box.name = <span class="string">&quot;sherry&quot;</span>;</span><br><span class="line">      box.attr.age = <span class="number">22</span>;</span><br><span class="line">      </span><br><span class="line">      <span class="built_in">console</span>.log(box);</span><br><span class="line">      <span class="comment">/**</span></span><br><span class="line"><span class="comment">       * &#123;</span></span><br><span class="line"><span class="comment">          name: &quot;doreen&quot;,</span></span><br><span class="line"><span class="comment">          attr: &#123;</span></span><br><span class="line"><span class="comment">              age: 22,</span></span><br><span class="line"><span class="comment">          &#125;,</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">       */</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> &#123; box, handleChange &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="isReadonly"><a href="#isReadonly" class="headerlink" title="isReadonly"></a>isReadonly</h2><blockquote>
<p>判断是否是<code>readonly</code>类型 返回布尔值</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">isReadonly(state) <span class="comment">// true / false</span></span><br></pre></td></tr></table></figure>

<h2 id="computed"><a href="#computed" class="headerlink" title="computed"></a>computed</h2><blockquote>
<p>使用getter函数，从getter返回的值返回一个不变的响应式<code>ref</code>对象</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">const</span> count = ref(<span class="number">1</span>)</span><br><span class="line"><span class="keyword">const</span> plusOne = computed(<span class="function">() =&gt;</span> count.value + <span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(plusOne.value) <span class="comment">// 2</span></span><br><span class="line">plusOne.value++ <span class="comment">// error</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p>通过<code>get</code>和<code>set</code>函数对象创建ref对象</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">let</span> msg = ref(<span class="string">&quot;title&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> str = computed(&#123;</span><br><span class="line">  get: <span class="function">() =&gt;</span> msg.value + <span class="string">&quot; get &quot;</span>,</span><br><span class="line">  set: <span class="function">(<span class="params">val</span>) =&gt;</span> &#123;</span><br><span class="line">    msg.value = val + <span class="string">&quot; set &quot;</span>;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(str.value);   <span class="comment">// title get</span></span><br><span class="line">str.value = <span class="string">&quot;set value&quot;</span>;  <span class="comment">// set value</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="watch"><a href="#watch" class="headerlink" title="watch"></a>watch</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; watch &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// ...</span></span><br><span class="line">    <span class="comment">// 多个观察对象 watch([val1,val2 ... ],([])=&gt;&#123; ... &#125;)</span></span><br><span class="line">    watch(msg, <span class="function">(<span class="params">newV, oldV</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="built_in">console</span>.log(newV, oldV);</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// ...</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="Teleport组件"><a href="#Teleport组件" class="headerlink" title="Teleport组件"></a>Teleport组件</h2><blockquote>
<p>vue项目在编译后所有的内容都是在 <code>&lt;div id=&#39;app&#39;&gt;&lt;/div&gt;</code> 标签下进行的，样式什么的会受到全局样式的影响</p>
</blockquote>
<blockquote>
<p><code>Teleport</code>是一种能够将我们的模板移动到 DOM 中 Vue app 之外的其他位置的技术</p>
</blockquote>
<h3 id="属性"><a href="#属性" class="headerlink" title="属性"></a>属性</h3><h4 id="to"><a href="#to" class="headerlink" title="to"></a>to</h4><blockquote>
<p><code>&lt;teleport to=&quot;#some-id / .some-class / [data-teleport]&quot; /&gt;</code> to属性后面必须是有效的查询选择器或 HTMLElement (如果在浏览器环境中使用)。目的是将<code>teleport</code>内的元素移动到该容器中</p>
</blockquote>
<h4 id="disabled"><a href="#disabled" class="headerlink" title="disabled"></a>disabled</h4><blockquote>
<p><code>&lt;teleport to=&#39;...&#39; :disabled=&#39;disabledFlag&#39;&gt;</code>此可选属性可用于禁用 <code>&lt;teleport&gt;</code> 的功能，这意味着其插槽内容将不会移动到任何位置，而是在您在周围父组件中指定了 <code>&lt;teleport&gt;</code> 的位置渲染</p>
</blockquote>
<h3 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h3><ol>
<li><p>public - index.html<br>在 <code>&lt;div id=&quot;app&quot;&gt;&lt;/div&gt;</code> 下方或者什么地方新增 <code>&lt;div id=&quot;example&quot;&gt;&lt;/div&gt;</code></p>
</li>
<li><p>新建一个 Example.vue 文件 并挂载到 App.vue 中进行展示</p>
</li>
</ol>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- Example.vue --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">teleport</span> <span class="attr">to</span>=<span class="string">&#x27;#example&#x27;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span> ... <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">teleport</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="css">  <span class="comment">/* ... */</span></span></span><br><span class="line"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- App.vue --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    ....</span><br><span class="line">    <span class="tag">&lt;<span class="name">example</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>这样Example.vue所写的内容就存在于<code>&lt;div id=&quot;example&quot;&gt;&lt;/div&gt;</code>标签中，而不是<code>&lt;div id=&quot;app&quot;&gt;&lt;/div&gt;</code>中，样式不会受app的影响</p>
<h2 id="Suspense异步组件"><a href="#Suspense异步组件" class="headerlink" title="Suspense异步组件"></a>Suspense异步组件</h2><blockquote>
<p>Suspense组件用于在等待某个异步组件解析时显示后备内容</p>
</blockquote>
<h3 id="使用-1"><a href="#使用-1" class="headerlink" title="使用"></a>使用</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"> <span class="comment">&lt;!-- Example.vue --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span> &#123;&#123;result&#125;&#125; <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&#x27;ts&#x27;</span>&gt;</span></span><br><span class="line">...</span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> defineComponent(&#123;</span></span><br><span class="line"><span class="javascript">  <span class="keyword">async</span> setup()&#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> rawData = <span class="keyword">await</span> axios.get(<span class="string">&#x27;...&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123; <span class="attr">result</span>:rowData.data &#125;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="css">  <span class="comment">/* ... */</span></span></span><br><span class="line"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">&lt;!-- App.vue --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">Suspense</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">template</span> #<span class="attr">default</span>&gt;</span></span><br><span class="line">      <span class="comment">&lt;!-- 请求成功后展示的组件 --&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">example</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">template</span> #<span class="attr">fallback</span>&gt;</span></span><br><span class="line">      <span class="comment">&lt;!-- 没请求的时候展示的组件 --&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Loading...<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">Suspense</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&#x27;ts&#x27;</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="comment">// 捕获请求异常</span></span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; onErrorCaptured &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span>&#123;</span></span><br><span class="line">  ...</span><br><span class="line">  setup()&#123;</span><br><span class="line"><span class="javascript">    <span class="comment">// 返回bool值</span></span></span><br><span class="line"><span class="javascript">    onErrorCaptured(<span class="function">(<span class="params">error</span>)=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">      <span class="built_in">console</span>.log(error)</span></span><br><span class="line"><span class="javascript">      <span class="keyword">return</span> <span class="literal">true</span></span></span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2020/10/09/vue3/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2020/11/20/%E4%BB%A3%E7%A0%81/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            代码段
          
        </div>
      </a>
    
    
      <a href="/2020/10/05/vue3%E5%8E%9F%E7%94%9F%E5%AE%9E%E7%8E%B0/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">vue3响应式的实现</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "9OLyDF6ymKbs1woL9kh2qbae-gzGzoHsz",
    app_key: "RTkmORaJHqkGjEovHixLK04x",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2019-2023
        <i class="ri-heart-fill heart_icon"></i> John Doe
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/avatar.webp" alt="Doreen&#39;s Blog"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->


<script src="/js/clickBoom2.js"></script>


<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


<script src="/js/dz.js"></script>



    
  </div>
</body>

</html>